package com.ibeeking.found.common.utils;

import com.ibeeking.nematos.utils.data.ArrayUtils;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

public class DesUtils {
    public static final String ALGORITHM_DES = "DES/CBC/PKCS5Padding";
    public static String DESIV;
    public static String appkey = "c306e6eb-fdba-11e7-9bb0-00163e0004bf";
    public static String sercret = "jZ0F9RTa5Y4NDZ95C4n38SuddBgtSw05";
    public static String key = "1559383106";

    public static String encode(String key2, String data) {
        try {
            return encode(key2, data.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String encode1(String key2, String data, String iv) throws Exception {
        return encode1(key2, data.getBytes(), iv);
    }

    public static String encode(String key2, byte[] data) throws Exception {
        try {
            SecretKey generateSecret = SecretKeyFactory.getInstance("DES").generateSecret(new DESKeySpec(key2.getBytes()));
            Cipher instance = Cipher.getInstance(ALGORITHM_DES);
            instance.init(1, generateSecret, new IvParameterSpec(DESIV.getBytes()));
            return Base64.getEncoder().encodeToString(instance.doFinal(data));
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

    public static String encode1(String key2, byte[] data, String sic) throws Exception {
        try {
            SecretKey generateSecret = SecretKeyFactory.getInstance("DES").generateSecret(new DESKeySpec(key2.getBytes()));
            Cipher instance = Cipher.getInstance(ALGORITHM_DES);
            instance.init(1, generateSecret, new IvParameterSpec(sic.getBytes()));
            return Base64.getEncoder().encodeToString(instance.doFinal(data));
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

    public static byte[] decode(String key2, byte[] data) {
        byte[] bArr = null;
        try {
            try {
                SecretKey generateSecret = SecretKeyFactory.getInstance("DES").generateSecret(new DESKeySpec(key2.getBytes()));
                Cipher instance = Cipher.getInstance(ALGORITHM_DES);
                instance.init(2, generateSecret, new IvParameterSpec(DESIV.getBytes()));
                bArr = instance.doFinal(data);
                return bArr;
            } catch (Exception e) {
                throw new Exception(e);
            }
        } catch (Throwable th) {
            return null;
        }
    }

    public static String decodeValue(String key2, String data) {
        try {
            byte[] decode = decode(key2, Base64.getDecoder().decode(data));

            if (ArrayUtils.isEmpty(decode)) {
                return "";
            }
            String str = new String(decode);
            if (!str.equals("")) {
                return str;
            } else {
                return "";
            }
        } catch (Exception e) {
            return "";
        }
    }

    public static String getkey(String time) {
        return new StringBuffer(md5Password(new StringBuffer(md5Password(time, 1)).toString().substring(0, 8).toUpperCase() + appkey, 1)).toString().substring(12, 20).toLowerCase();
    }

    public static String getiv(String time) {
        return new StringBuffer(md5Password(new StringBuffer(md5Password(time, 1)).substring(12, 20).toLowerCase() + sercret, 1)).substring(24, 32).toUpperCase();
    }

    public static String md5Password(String password, int code) {
        try {
            byte[] digest = MessageDigest.getInstance("md5").digest(password.getBytes());
            StringBuffer stringBuffer = new StringBuffer();
            for (byte b : digest) {
                String hexString = Integer.toHexString(b & 255);
                if (hexString.length() == 1) {
                    stringBuffer.append("0");
                }
                stringBuffer.append(hexString);
            }
            return stringBuffer.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String decode(String str) {
        DESIV = getiv(key);
        return encode(getkey(key), str);
    }

    public static String encode(String str) {
        DESIV = getiv(key);
        return decodeValue(getkey(key), str);
    }

    public static void main(String[] args) throws Exception {
        String str = key;
        DESIV = getiv(str);
        String encode = encode(getkey(str), "18739941307");
        System.out.println("密文" + encode);
        System.out.println("333:" + decodeValue(getkey(str), encode));
        System.out.println("解码" + getTel(encode, key));
        System.out.println("加密" + decode("root"));
        System.out.println("解密" + encode("SMy3f94QGFM="));
    }

    public static String getTel(String str_tel, String str_time) {
        DESIV = getiv(str_time);
        return decodeValue(getkey(str_time), str_tel);
    }
}
